/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import DBConnectionPackage.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Don
 */
public class CustomerManager {
    
    
    public Customer checkCustomer(String username,String password) 
    {
        Hasher hasher = new Hasher();
        Customer result = null;
        
        String hashpass = new Hasher().Hash(password);

        try
        {
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();


                int i = 1;

                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ? ");

                pstmt.setString(i++, username);
                pstmt.setString(i++, hashpass);

                ResultSet rs = pstmt.executeQuery();

                int x;
               
                while(rs.next())
                { 
                   result = new Customer();
                   result.setFirstname(rs.getString("firstName"));
                   result.setLastname(rs.getString("lastName"));
                   result.setMiddlename(rs.getString("midName"));
                   result.setUsername(rs.getString("username"));
                   result.setUserType(rs.getInt("userType"));
                   
                   return result;
                }
                conn.close();
            }
        
        catch (SQLException ex)
        { 
            ex.printStackTrace();
        }
        return result;       
    }
    
    public boolean addCustomer(Customer newCustomer) 
    {
        boolean result = false;

        try{

            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
    
            int i = 1, j=1, k = 1, newid = 0;

            String username = newCustomer.getUsername();
            
             Hasher hasher = new Hasher();
        
             String hashpass = new Hasher().Hash(newCustomer.getPassword());

            //SQL query
            PreparedStatement pstmtUser = conn.prepareStatement ("INSERT INTO user(username, password, firstName, midName, lastName, email, homeAddress, userType) VALUES(?,?,?,?,?,?,?,?)");

            
        
           
            pstmtUser.setString(i++, newCustomer.getUsername());
            pstmtUser.setString(i++, hashpass);
            pstmtUser.setString(i++, newCustomer.getFirstname());
            pstmtUser.setString(i++, newCustomer.getMiddlename()); 
            pstmtUser.setString(i++, newCustomer.getLastname()); 
            pstmtUser.setString(i++, newCustomer.getEmail()); 
            pstmtUser.setString(i++, newCustomer.getAddress()); 
            pstmtUser.setString(i++, "0");
            pstmtUser.executeUpdate();
            //include parameters
            
           
          /*  PreparedStatement pstmtUserID = conn.prepareStatement("SELECT last_insert_id() from user");
            stmtUserID
            //ResultSet rsUserID=pstmtUserID.executeQuery();
        /*    System.out.println(RETURN_GENERATED_KEYS(pstmtUser));
            ResultSet rsUserId = pstmtUser.getGeneratedKeys();
            System.out.println(rsUserId);
            if(rsUserId != null && rsUserId.next())
            {
                newid = rsUserId.getInt(1);
                System.out.println(rsUserId);
            }
             */
            PreparedStatement pstmtCustomer = conn.prepareStatement("INSERT INTO customer(nameCard,cardNum,cardType,cardExpiration) VALUES (?,?,?,?)"); 
          
            pstmtCustomer.setString(k++, newCustomer.getCreditcardname());
            pstmtCustomer.setString(k++, newCustomer.getCreditcardnumber());
            pstmtCustomer.setString(k++, newCustomer.getCreditcardtype());
            pstmtCustomer.setString(k++, newCustomer.getCreditcardexpdate());
         
            pstmtCustomer.executeUpdate();
           
           //execute query
            
             result = true;

            //close DB connection
            conn.close();
            

            
            }
        catch (SQLException ex)
        { ex.printStackTrace();}

        return result;
    }
    public boolean changepasswordCustomer(String username, String oldpassword, String newpassword, String repassword){
          boolean result= false;
          int i =1;

          Hasher hasher = new Hasher();
          String hashopass = new Hasher().Hash(oldpassword);
          String hashnpass = new Hasher().Hash(newpassword);
          String hashrpass = new Hasher().Hash(repassword);
          String stored = null;
          
          try{
              
            if(hashnpass.equals(hashrpass)&& !hashopass.equals(hashnpass))
            {
                
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            int j = 1;
            PreparedStatement pstmt = conn.prepareStatement("SELECT password FROM user WHERE username = ?");
            pstmt.setString(j++, username);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next())
            {
                stored = rs.getString("password"); 
            }
            
            if(stored.equals(hashopass))
            {
                
                PreparedStatement pstmtCP = conn.prepareStatement ("UPDATE user SET password = ? where username = ?");

                pstmtCP.setString(i++, hashnpass);
                pstmtCP.setString(i++, username);

                pstmtCP.executeQuery();
                System.out.println("pasok");
                return result = true;
             }
            }
        }    
        catch (SQLException ex)
        { ex.printStackTrace();}

        return result;
          
   }
    
    
    
    
}
